# 0.0 Load data ----
source('src/initialize.r')

# 1.0 Figure 1 ----
therm_change_plot <-
  ds %>%
  filter(!is.na(feeling_change)) %>%
  group_by(full_treatment) %>%
  summarise(
    nrow = length(feeling_change),
    mean = mean(feeling_change),
    se = sd(feeling_change) / sqrt(nrow),
    ci = 1.96 * se,
    label = format(round(mean, 2), nsmall = 2)
  ) %>%
  ggplot(., aes(x = full_treatment, y = mean)) +
  geom_errorbar(
    aes(ymin = mean - ci, ymax = mean + ci),
    position = position_dodge(width = .5),
    width = 0,
    linewidth = 1
  ) +
  geom_point(size = 3) +
  geom_text(
    aes(x = full_treatment, y = mean - ci, label = label),
    vjust = 1.5,
    size = 3.5
  ) +
  labs(
    y = "Mean Feeling Therm. Change",
    title = "Feeling Thermometer (0-100)",
    x = "Treatment Condition"
  ) +
  scale_x_discrete(
    labels = c(
      "Control" = "Control",
      "Announce" = "Announce",
      "Credit_Claim" = "Credit\nClaim",
      "Commiserate" = "Commiserate",
      "Protect" = "Protect"
    )
  ) +
  scale_y_continuous(limits = c(0, 12.5))

effect_change_plot <-
  ds %>%
  filter(!is.na(effective_change)) %>%
  group_by(full_treatment) %>%
  summarise(
    nrow = length(effective_change),
    mean = mean(effective_change),
    se = sd(effective_change) / sqrt(nrow),
    ci = 1.96 * se,
    label = format(round(mean, 2), nsmall = 2)
  ) %>%
  ggplot(., aes(x = full_treatment, y = mean)) +
  geom_pointrange(
    aes(ymin = mean - ci, ymax = mean + ci),
    position = position_dodge(width = .5),
    size = 1,
    linewidth = 1
  ) +
  geom_text(
    aes(x = full_treatment, y = mean - ci, label = label),
    vjust = 1.5,
    size = 3.5
  ) +
  labs(
    y = "Mean Effectiveness Change",
    title = "Effectiveness Rating (0-10)",
    x = "Treatment Condition"
  ) +
  scale_x_discrete(
    labels = c(
      "Control" = "Control",
      "Announce" = "Announce",
      "Credit_Claim" = "Credit\nClaim",
      "Commiserate" = "Commiserate",
      "Protect" = "Protect"
    )
  ) +
  scale_y_continuous(limits = c(-.2, 1.15))

represent_change_plot <-
  ds %>%
  filter(!is.na(represent_change)) %>%
  group_by(full_treatment) %>%
  summarise(
    nrow = length(represent_change),
    mean = mean(represent_change),
    se = sd(represent_change) / sqrt(nrow),
    ci = 1.96 * se,
    label = format(round(mean, 2), nsmall = 2)
  ) %>%
  ggplot(., aes(x = full_treatment, y = mean)) +
  geom_pointrange(
    aes(ymin = mean - ci, ymax = mean + ci),
    position = position_dodge(width = .5),
    size = 1,
    linewidth = 1
  ) +
  geom_text(
    aes(x = full_treatment, y = mean - ci, label = label),
    vjust = 1.5,
    size = 3.5
  ) +
  labs(
    y = "Mean Representation Change",
    title = "Representation Rating (0-10)",
    x = "Treatment Condition"
  ) +
  scale_x_discrete(
    labels = c(
      "Control" = "Control",
      "Announce" = "Announce",
      "Credit_Claim" = "Credit\nClaim",
      "Commiserate" = "Commiserate",
      "Protect" = "Protect"
    )
  ) +
  scale_y_continuous(limits = c(-.2, 1.15))

responsible_change_plot <-
  ds %>%
  filter(!is.na(responsible_change)) %>%
  group_by(full_treatment) %>%
  summarise(
    nrow = length(responsible_change),
    mean = mean(responsible_change),
    se = sd(responsible_change) / sqrt(nrow),
    ci = 1.96 * se,
    label = format(round(mean, 2), nsmall = 2)
  ) %>%
  ggplot(., aes(x = full_treatment, y = mean)) +
  geom_pointrange(
    aes(ymin = mean - ci, ymax = mean + ci),
    position = position_dodge(width = .5),
    size = 1,
    linewidth = 1
  ) +
  geom_text(
    aes(x = full_treatment, y = mean - ci, label = label),
    vjust = 1.5,
    size = 3.5
  ) +
  labs(
    y = "Mean Responsibility Change",
    title = "Responsibility Rating (0-10)",
    x = "Treatment Condition"
  ) +
  scale_x_discrete(
    labels = c(
      "Control" = "Control",
      "Announce" = "Announce",
      "Credit_Claim" = "Credit\nClaim",
      "Commiserate" = "Commiserate",
      "Protect" = "Protect"
    )
  ) +
  scale_y_continuous(limits = c(-.2, 1.15))

combined_mean_plots <-
  therm_change_plot +
  effect_change_plot +
  represent_change_plot +
  responsible_change_plot +
  plot_annotation(
    caption = "NOTE: 95 percent confidence intervals surround point estimates."
  ) &
  theme(plot.caption = element_text(size = 12))

combined_mean_plots

# 2.0 Table 3 ----
t.test.h1.therm <- t.test(
  ds$feeling_change[ds$full_treatment == 'Protect'],
  ds$feeling_change[ds$full_treatment == 'Credit_Claim']
)
t.test.h1.eff <- t.test(
  ds$effective_change[ds$full_treatment == 'Protect'],
  ds$effective_change[ds$full_treatment == 'Credit_Claim']
)
t.test.h1.rep <- t.test(
  ds$represent_change[ds$full_treatment == 'Protect'],
  ds$represent_change[ds$full_treatment == 'Credit_Claim']
)
t.test.h1.resp <- t.test(
  ds$responsible_change[ds$full_treatment == 'Protect'],
  ds$responsible_change[ds$full_treatment == 'Credit_Claim']
)

data.frame(
  "DV" = c(
    "Feeling Therm. Rating (0-100) Change",
    "Effectiveness Rating (0-10) Change",
    "Representation Rating (0-10) Change",
    "Responsibility Rating (0-10) Change"
  ),
  "Credit Claim" = c(
    t.test.h1.therm$estimate[[2]],
    t.test.h1.eff$estimate[[2]],
    t.test.h1.rep$estimate[[2]],
    t.test.h1.resp$estimate[[2]]
  ),
  "Protect" = c(
    t.test.h1.therm$estimate[[1]],
    t.test.h1.eff$estimate[[1]],
    t.test.h1.rep$estimate[[1]],
    t.test.h1.resp$estimate[[1]]
  ),
  "Diff in Means" = c(
    t.test.h1.therm$estimate[[1]] - t.test.h1.therm$estimate[[2]],
    t.test.h1.eff$estimate[[1]] - t.test.h1.eff$estimate[[2]],
    t.test.h1.rep$estimate[[1]] - t.test.h1.rep$estimate[[2]],
    t.test.h1.resp$estimate[[1]] - t.test.h1.resp$estimate[[2]]
  ),
  "p.value" = c(
    t.test.h1.therm$p.value,
    t.test.h1.eff$p.value,
    t.test.h1.rep$p.value,
    t.test.h1.resp$p.value
  )
)

# 3.0 Table 4 ----
t.test.h2.therm <- t.test(
  ds$feeling_change[ds$full_treatment == 'Commiserate'],
  ds$feeling_change[ds$full_treatment == 'Control']
)
t.test.h2.eff <- t.test(
  ds$effective_change[ds$full_treatment == 'Commiserate'],
  ds$effective_change[ds$full_treatment == 'Control']
)
t.test.h2.rep <- t.test(
  ds$represent_change[ds$full_treatment == 'Commiserate'],
  ds$represent_change[ds$full_treatment == 'Control']
)
t.test.h2.resp <- t.test(
  ds$responsible_change[ds$full_treatment == 'Commiserate'],
  ds$responsible_change[ds$full_treatment == 'Control']
)

data.frame(
  "DV" = c(
    "Feeling Therm. Rating (0-100) Change",
    "Effectiveness Rating (0-10) Change",
    "Representation Rating (0-10) Change",
    "Responsibility Rating (0-10) Change"
  ),
  "Control" = c(
    t.test.h2.therm$estimate[[2]],
    t.test.h2.eff$estimate[[2]],
    t.test.h2.rep$estimate[[2]],
    t.test.h2.resp$estimate[[2]]
  ),
  "Commiseration" = c(
    t.test.h2.therm$estimate[[1]],
    t.test.h2.eff$estimate[[1]],
    t.test.h2.rep$estimate[[1]],
    t.test.h2.resp$estimate[[1]]
  ),
  "Diff in Means" = c(
    t.test.h2.therm$estimate[[1]] - t.test.h2.therm$estimate[[2]],
    t.test.h2.eff$estimate[[1]] - t.test.h2.eff$estimate[[2]],
    t.test.h2.rep$estimate[[1]] - t.test.h2.rep$estimate[[2]],
    t.test.h2.resp$estimate[[1]] - t.test.h2.resp$estimate[[2]]
  ),
  "p.value" = c(
    t.test.h2.therm$p.value,
    t.test.h2.eff$p.value,
    t.test.h2.rep$p.value,
    t.test.h2.resp$p.value
  )
)

# 4.0 Figure 2 ----
fig2.model <-
  summary(
    lm(
      feeling_change ~ full_treatment,
      data = ds %>%
        filter(full_treatment %in% c('Announce', 'Commiserate')) %>%
        droplevels()
    )
  )

h3.null.test.alt <-
  data.frame(
    estimate = fig2.model$coefficients[2, 1],
    std.error = fig2.model$coefficients[2, 2]
  ) %>%
  mutate(
    ci.lower = estimate - 1.645 * std.error,
    ci.upper = estimate + 1.645 * std.error
  ) %>%
  ggplot(.) +
  geom_point(aes(x = NA, y = estimate), size = 3) +
  geom_errorbar(aes(x = NA, ymin = ci.lower, ymax = ci.upper), width = 0.2) +
  geom_text(
    aes(
      y = estimate,
      x = NA,
      label = glue::glue('[{round(ci.lower,2)}, {round(ci.upper,2)}]')
    ),
    size = 5,
    vjust = -1.25
  ) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  geom_hline(yintercept = -3, linetype = "solid", color = "blue") +
  geom_hline(yintercept = 3, linetype = "solid", color = "blue") +
  labs(
    title = "Commiserate Treatment vs. Announce Treatment",
    y = "Difference of Means Estimate\n(Feeling Thermometer Rating Change)",
    x = "Commiserate - Announce"
  ) +
  scale_y_continuous(
    limits = c(-3.5, 3.5),
    breaks = c(-3, -2, -1, 0, 1, 2, 3)
  ) +
  coord_flip() +
  theme(axis.text.y = element_blank(), axis.ticks.y = element_blank())

h3.null.test.alt
